package com.sgb.zhaocai.view.demand.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.google.android.material.tabs.TabLayout;
import com.sgb.zhaocai.R;
import com.sgb.zhaocai.databinding.FragmentWebDemandForecastBinding;
import com.sgb.zhaocai.entity.DemandForecastListBean;
import com.sgb.zhaocai.entity.DemandForecastListEntity;
import com.sgb.zhaocai.view.demand.adapter.DemandListAdapter;
import com.sgb.zhaocai.viewmodel.WebDemandForecastModel;
import com.swgk.core.base.AppLazyFragment;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.List;

/**
 * Time:2021/9/1
 * AUTHOR:F_Q
 * Description: 需求预告企业网站
 */
public class WebDemandForecastFragment extends AppLazyFragment {

    private FragmentWebDemandForecastBinding binding;
    private String[] mTitles = {"材料采购", "租赁需求", "劳务用工", "分包计划"};
    private WebDemandForecastModel mModel;
    private DemandListAdapter demandlistadapter;
    private boolean isLoadMore = false;
    private DemandForecastListBean requestBean = new DemandForecastListBean();
    private int position = 0;

    public static final String COMPANYNO = "companyNo";

    public static WebDemandForecastFragment newInstance(String companyno) {
        WebDemandForecastFragment fragment = new WebDemandForecastFragment();
        Bundle bundle = new Bundle();
        bundle.putString(COMPANYNO, companyno);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_web_demand_forecast, container, false);
        mModel = new ViewModelProvider(this).get(WebDemandForecastModel.class);
        return binding.getRoot();
    }

    @Override
    protected void initView() {
        super.initView();
        String companyno = getArguments().getString(COMPANYNO);
        if (!TextUtils.isEmpty(companyno)){
            requestBean.setComNo(companyno);
        }else {
            requestBean.setComNo("");
        }
        requestBean.setStatus("1");
        for (int i = 0; i < mTitles.length; i++) {
            TabLayout.Tab tab = binding.tabLayout.newTab();
            tab.setText(mTitles[i]);
            binding.tabLayout.addTab(tab);
        }
        binding.tabLayout.getTabAt(0).select();
        binding.tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                position = tab.getPosition();
                initDataList();
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        if (demandlistadapter == null) {
            demandlistadapter = new DemandListAdapter(getActivity(), null);
        }
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        binding.recyclerView.setLayoutManager(layoutManager);
        binding.recyclerView.setHasFixedSize(true);
        //解决数据加载完成后, 没有停留在顶部的问题
        binding.recyclerView.setFocusable(false);
        binding.recyclerView.setAdapter(demandlistadapter);
        binding.recyclerView.setLoadingMoreEnabled(true);
        demandlistadapter.setOnItemClick(new AdapterOnItemClick<DemandForecastListEntity>() {
            @Override
            public void onItemClick(DemandForecastListEntity demandForecastListEntity, int position) {
                mModel.getWebH5(WebDemandForecastFragment.this, demandForecastListEntity.getId(), demandForecastListEntity.getModuleType());
            }
        });
        demandlistadapter.setOnItemStateOperate(new DemandListAdapter.OnItemStateOperate() {
            @Override
            public void OnItem(DemandForecastListEntity entity) {
                mModel.getWebH5(WebDemandForecastFragment.this, entity.getId(), entity.getModuleType());
            }
        });
        binding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                initDataList();
            }

            @Override
            public void onLoadMore() {
                isLoadMore = true;
                requestBean.page++;
                //上拉加载更多
                mModel.getMaterialHomeList(WebDemandForecastFragment.this, position, requestBean);
            }
        });
    }



    @Override
    protected void onLazyLoad() {
        initDataList();
    }

    private void initDataList() {
        isLoadMore = false;
        requestBean.page = 1;
        mModel.getMaterialHomeList(WebDemandForecastFragment.this, position, requestBean);
    }

    private void RecyclerRefreshing() {
        binding.recyclerView.refreshComplete();
        binding.recyclerView.loadMoreComplete();
    }

    /**
     * 列表返回数据
     *
     * @param data
     */
    public void setDemandList(List<DemandForecastListEntity> data,boolean isLastPage) {
        RecyclerRefreshing();
        if (isLoadMore) {
            if (data != null && data.size() > 0) {
                demandlistadapter.addDatas(data);
            }
            binding.recyclerView.setNoMore(isLastPage);
        } else {
            if (data != null && data.size() > 0) {
                binding.emptyView.setVisibility(View.GONE);
                binding.recyclerView.setVisibility(View.VISIBLE);
                demandlistadapter.setDatas(data);
                binding.recyclerView.scrollToPosition(0);
                binding.recyclerView.setNoMore(isLastPage);
            } else {
                demandlistadapter.clearDatas();
                binding.emptyView.setVisibility(View.VISIBLE);
                binding.recyclerView.setVisibility(View.GONE);
            }
        }
    }

}
